package com.study.controller;

import com.study.pojo.Books;
import com.study.service.bookService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class bookController {
    @Autowired
    @Qualifier("bookServiceImp")
    private bookService bookService;
    //查询全部书籍
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
    //跳转到添加图书页面
    @RequestMapping("/toaddBook")
    public String add(){
        return "addBookPage";
    }
    //添加图书
    @RequestMapping("/addBook")
    public String addbook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    //删除图书页面
    @RequestMapping("/delBookPage")
    public String delbook(){
        return "delBook";
    }
    //删除图书
    @RequestMapping("/delBook")
    public String delbook(int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
    //根据id找图书
    @RequestMapping("/queryBook")
    public String querybook(int id,Model model){
        Books book = bookService.queryBookById(id);
        model.addAttribute("QBook",book);
        return "updateBook";
    }
    @RequestMapping("/updateBook")
    public String updatebook(Books books){
        System.out.println(books.toString());
        int i = bookService.updateBook(books);
        return "redirect:/book/allBook";
    }
    //根据名字模糊查图书
    @RequestMapping("/queryBookByname")
    public String querybookbyname(String bookName,Model model){
        List<Books> list = bookService.queryBookBylike(bookName);
        model.addAttribute("list",list);
        return "allBook";
//        model.addAttribute("QBook",book);
//        return "updateBook";
    }
}
